package com.yh.autobio.modules.adatper;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yh.autobio.R;
import com.yh.autobio.common.ArithUtil;
import com.yh.autobio.common.StringUtils;
import com.yh.autobio.component.dialog.DialogHelp;
import com.yh.autobio.gddb.gbmanager.BackMatManager;
import com.yh.autobio.gddb.gdbean.BackMatBean;
import com.yh.autobio.modules.recycleviewutil.onMoveAndSwipedListener;
import com.yh.autobio.modules.recycleviewutil.onStateChangedListener;

import java.math.BigDecimal;
import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * =========================
 * 版权所有，违法必究
 * 作者：john
 * <p>
 * 工程：FoodBarcode
 * <p>
 * 文件名：ADDBackMatAdapter
 * <p>
 * 创建时间：2017.06.03.10:03
 * *****************************
 */
public class ADDBackMatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements onMoveAndSwipedListener {


    private Context mContext;
    private ArrayList<BackMatBean> workInfos;
    private OnSaleAddAdapterListern listener;
    private String type;

    public ADDBackMatAdapter(Context context, ArrayList<BackMatBean> checkedCKS, OnSaleAddAdapterListern listener) {
        this.mContext = context;
        this.workInfos = checkedCKS;
        this.listener = listener;
    }

    public ADDBackMatAdapter(Context context, ArrayList<BackMatBean> checkedCKS, OnSaleAddAdapterListern listener, String type) {
        this.mContext = context;
        this.workInfos = checkedCKS;
        this.listener = listener;
        this.type = type;
    }

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        return false;
    }

    @Override
    public void onItemDismiss(int position) {
        //删除mItems数据
        workInfos.remove(position);
        //删除RecyclerView列表对应item
        notifyItemRemoved(position);
    }

    public void onItemDismiss(int position, BackMatBean bean) {

        if ("show".equals(type)) {
            BackMatManager.getInstance(mContext).delMat(bean);//提交成功后删除缓存
        }
        //删除mItems数据
        workInfos.remove(position);
        //删除RecyclerView列表对应item
        notifyItemRemoved(position);
    }


//    public void add(int position) {
//        workInfos.add(position, text);
//        notifyItemInserted(position);
//    }

    public interface OnSaleAddAdapterListern {
        void onFHCKClick(BackMatBean bean, int position);

        void onDwClick(BackMatBean bean, TextView tvUnit, int position);

        void onMoneyChanged();
    }

    public ArrayList<BackMatBean> getBackList() {
        return workInfos;
    }

    public void setBackList(ArrayList<BackMatBean> workInfos) {
        this.workInfos = workInfos;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_sale_out, parent,
                false);
        return new BackViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof BackViewHolder) {
            BackMatBean backMatBean = workInfos.get(position);
            initDataView((BackViewHolder) holder, backMatBean, position);
        }
    }

    private void initDataView(final BackViewHolder holder, final BackMatBean bean, final int position) {
        double v = 0;
        holder.llJskc.setVisibility(View.GONE);
        holder.llBz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final View view1 = View.inflate(mContext, R.layout.dialog_change_mat, null);
                final TextView title = (TextView) view1.findViewById(R.id.tv_title);
                final EditText content = (EditText) view1.findViewById(R.id.et_content);
                content.setInputType(InputType.TYPE_CLASS_TEXT);
                title.setText(bean.getName() + " -- " + "备注说明");
                String s = bean.getFNote();
                if (!StringUtils.isEmpty(s)) {
                    content.setText(s.trim());
                    content.setSelection(s.length());
                }
                DialogHelp.getConfirmDialog(mContext, view1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String trim = content.getText().toString().toString();
                        bean.setFNote(trim);
                        holder.tvNote.setText(trim);
                    }
                }).show();
            }
        });
        holder.llXsje.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final View view1 = View.inflate(mContext, R.layout.dialog_change_mat, null);
                final TextView title = (TextView) view1.findViewById(R.id.tv_title);
                final EditText content = (EditText) view1.findViewById(R.id.et_content);
                content.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                title.setText(bean.getName() + " -- " + "销售金额");
                String s = holder.tvSalesMoney.getText().toString().trim();
                if (!StringUtils.isEmpty(s)) {
                    content.setText(s);
                    content.setSelection(s.length());
                }
                DialogHelp.getConfirmDialog(mContext, view1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String trim = content.getText().toString().toString();
                        double d;
                        try {
                            double v1 = Double.parseDouble(trim);
                            d = v1;
                            bean.setAllMoney(d);
                            holder.tvSalesMoney.setText(v1 + "");
                        } catch (Exception e) {
                            d = 0;
                            bean.setAllMoney(d);
                            holder.tvSalesMoney.setText("0");
                        }
                        bean.setAllMoney(d);

                    }
                }).show();
            }
        });
        holder.tvSalesMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (listener != null) {
                    listener.onMoneyChanged();
                }
//                String trim = editable.toString().trim();
//                double d;
//                try {
//                    double v1 = Double.parseDouble(trim);
//                    d = v1;
//                } catch (Exception e) {
//                    d = 0;
//                    holder.tvSalesMoney.setText("0");
//                }
//                bean.setAllMoney(d);
//                if (listener != null) {
//                    listener.onMoneyChanged();
//                }
            }
        });
        if (!StringUtils.isEmpty(bean.getTDCStockID())) {
            holder.tvFhWare.setText(bean.getTDCStockName());//发货仓库
        } else {
            holder.tvFhWare.setText(bean.getFDCStockName());//发货仓库
        }

        if ("0".equals(bean.getUnitType())) {//0代表baseName 1代表unit_name
            holder.tvUnit.setText(bean.getBaseName());
            holder.tvSalesSingle.setText(bean.getReprice() + "");
            v = Double.parseDouble(bean.getCount()) * bean.getReprice();
            bean.setAllMoney(ArithUtil.round(v, 0));
        } else if ("1".equals(bean.getUnitType())) {
            holder.tvUnit.setText(bean.getUnit_name());
            holder.tvSalesSingle.setText(bean.getSaleprice() + "");
            v = Double.parseDouble(bean.getCount()) * bean.getSaleprice();
            bean.setAllMoney(ArithUtil.round(v, 0));
        }

        holder.tvCcCode.setText(bean.getNumber());
        holder.tvSfNum.setText(bean.getCount());
        holder.tvGgModle.setText(bean.getModel());
        holder.tvNote.setText(bean.getFNote());

        holder.tvCcName.setText(bean.getName());
        double f = v;
        BigDecimal b = new BigDecimal(f);
        double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        holder.tvSalesMoney.setText("" + f1);//单个不五入
        holder.llJF.setVisibility(View.GONE);
//        holder.tvNote.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable editable) {
//                String note = editable.toString().trim();
//                bean.setFNote(note + "");
//            }
//        });
        holder.llXsdj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final View view1 = View.inflate(mContext, R.layout.dialog_change_mat, null);
                final TextView title = (TextView) view1.findViewById(R.id.tv_title);
                final EditText content = (EditText) view1.findViewById(R.id.et_content);
                content.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                title.setText(bean.getName() + " -- " + "销售单价");
                String s = holder.tvSalesSingle.getText().toString().trim();
                if (!StringUtils.isEmpty(s)) {
                    content.setText(s);
                    content.setSelection(s.length());
                }
                DialogHelp.getConfirmDialog(mContext, view1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String trim = content.getText().toString().toString();
                        double price;
                        if (!".".equals(trim)) {
                            if (StringUtils.isEmpty(trim)) {
                                price = 0;
                            } else {
                                price = Double.parseDouble(trim);
                            }
                            if ("0".equals(bean.getUnitType())) {//0代表baseName 1代表unit_name
                                bean.setReprice(price);
                            } else if ("1".equals(bean.getUnitType())) {
                                bean.setSaleprice(price);
                            }
                            holder.tvSalesSingle.setText(price + "");
                            double round = ArithUtil.round(Double.parseDouble(bean.getCount()) * price, 0);
                            bean.setAllMoney(round);
                            holder.tvSalesMoney.setText("" + Double.parseDouble(bean.getCount()) * price);
                        } else {
                            if ("0".equals(bean.getUnitType())) {//0代表baseName 1代表unit_name
                                bean.setReprice(0);
                            } else if ("1".equals(bean.getUnitType())) {
                                bean.setSaleprice(0);
                            }
                            holder.tvSalesSingle.setText("0");
                            bean.setAllMoney(0);
                            holder.tvSalesMoney.setText("0");
                            holder.tvSalesSingle.setText("0");
                        }
                    }
                }).show();

            }
        });
        holder.llSfsl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final View view1 = View.inflate(mContext, R.layout.dialog_change_mat, null);
                final TextView title = (TextView) view1.findViewById(R.id.tv_title);
                final EditText content = (EditText) view1.findViewById(R.id.et_content);
//                content.setInputType(InputType.TYPE_CLASS_NUMBER);
                title.setText(bean.getName() + " -- " + "实发数量");
                String s = holder.tvSfNum.getText().toString().trim();
                if (!StringUtils.isEmpty(s)) {
                    String[] split = s.split("=");
                    String s1 = split[0];
                    content.setText(s1);
                    content.setSelection(s1.length());
                }
                DialogHelp.getConfirmDialog(mContext, view1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String text = content.getText().toString().toString();
                        if (StringUtils.isEmpty(text)) {
                            bean.setCount("0");
                            holder.tvSfNum.setText("0");
                            holder.tvSalesMoney.setText("0");
                            bean.setAllMoney(0);
                            holder.tvSfNum.setText("0");
                        } else {
                            try {
                                double eval = StringUtils.eval(text);
                                bean.setCount(eval + "");
                                holder.tvSfNum.setText(text + "=" + eval);
//                                double count = Double.parseDouble(text);
                                if ("0".equals(bean.getUnitType())) {//0代表baseName 1代表unit_name
                                    double round = ArithUtil.round((bean.getReprice() * eval), 0);
                                    bean.setAllMoney(round);

                                    double f = bean.getReprice() * eval;
                                    BigDecimal b = new BigDecimal(f);
                                    double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

                                    holder.tvSalesMoney.setText("" + f1);//单个不五入
                                } else if ("1".equals(bean.getUnitType())) {
                                    double round1 = ArithUtil.round((bean.getSaleprice() * eval), 0);
                                    bean.setAllMoney(round1);

                                    double f = bean.getSaleprice() * eval;
                                    BigDecimal b = new BigDecimal(f);
                                    double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                                    holder.tvSalesMoney.setText("" + f1);//单个不五入
                                }
                            } catch (Exception e) {
                                Snackbar.make(((Activity) mContext).getWindow().getDecorView(), "输入公式格式错误，请重新输入", Snackbar.LENGTH_SHORT).show();
                            }
//                            if (!".".equals(text)) {
//                                bean.setCount(text);
//                                holder.tvSfNum.setText(text);
//                                double count = Double.parseDouble(text);
//                                if ("0".equals(bean.getUnitType())) {//0代表baseName 1代表unit_name
//                                    double round = ArithUtil.round((bean.getReprice() * count), 0);
//                                    bean.setAllMoney(round);
//
//                                    double f = bean.getReprice() * count;
//                                    BigDecimal b = new BigDecimal(f);
//                                    double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//
//                                    holder.tvSalesMoney.setText("" + f1);//单个不五入
//                                } else if ("1".equals(bean.getUnitType())) {
//                                    double round1 = ArithUtil.round((bean.getSaleprice() * count), 0);
//                                    bean.setAllMoney(round1);
//
//                                    double f = bean.getSaleprice() * count;
//                                    BigDecimal b = new BigDecimal(f);
//                                    double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//                                    holder.tvSalesMoney.setText("" + f1);//单个不五入
//                                }
//                            } else {
//                                bean.setCount("0");
//                                holder.tvSfNum.setText("0");
//                                holder.tvSalesMoney.setText("0");
//                                bean.setAllMoney(0);
//                                holder.tvSfNum.setText("0");
//                            }
                        }
                    }
                }).show();
            }
        });
//        holder.tvSfNum.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable editable) {
//                String text = editable.toString().trim();
//                if (StringUtils.isEmpty(text)) {
//                    bean.setCount("0");
//                    holder.tvSfNum.setText("0");
//                    holder.tvSalesMoney.setText("0");
//                    bean.setAllMoney(0);
//                } else {
//                    if (!".".equals(text)) {
//                        bean.setCount(text);
//                        double i = Double.parseDouble(text);
//                        if ("0".equals(bean.getUnitType())) {//0代表baseName 1代表unit_name
//                            double round = ArithUtil.round((bean.getReprice() * i), 0);
//                            bean.setAllMoney(round);
//
//                            double f = bean.getReprice() * i;
//                            BigDecimal b = new BigDecimal(f);
//                            double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//
//                            holder.tvSalesMoney.setText("" + f1);//单个不五入
//                        } else if ("1".equals(bean.getUnitType())) {
//                            double round1 = ArithUtil.round((bean.getSaleprice() * i), 0);
//                            bean.setAllMoney(round1);
//
//                            double f = bean.getSaleprice() * i;
//                            BigDecimal b = new BigDecimal(f);
//                            double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//                            holder.tvSalesMoney.setText("" + f1);//单个不五入
//                        }
//                    } else {
//                        bean.setCount("0");
//                        holder.tvSfNum.setText("0");
//                        holder.tvSalesMoney.setText("0");
//                        bean.setAllMoney(0);
//                    }
//                }
//            }
//        });
//        holder.tvSalesSingle.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable editable) {
//                String trim = editable.toString().trim();
//                double price;
//                if (!".".equals(trim)) {
//                    if (StringUtils.isEmpty(trim)) {
//                        price = 0;
//                    } else {
//                        price = Double.parseDouble(trim);
//                    }
//                    if ("0".equals(bean.getUnitType())) {//0代表baseName 1代表unit_name
//                        bean.setReprice(price);
//                    } else if ("1".equals(bean.getUnitType())) {
//                        bean.setSaleprice(price);
//                    }
//                    double round = ArithUtil.round(Double.parseDouble(bean.getCount()) * price, 0);
//                    holder.tvSalesMoney.setText("" + Double.parseDouble(bean.getCount()) * price);
//                    bean.setAllMoney(round);
//                } else {
//                    if ("0".equals(bean.getUnitType())) {//0代表baseName 1代表unit_name
//                        bean.setReprice(0);
//                    } else if ("1".equals(bean.getUnitType())) {
//                        bean.setSaleprice(0);
//                    }
//                    holder.tvSalesSingle.setText("0");
//                    holder.tvSalesMoney.setText("0");
//                    bean.setAllMoney(0);
//                }
//            }
//        });

        holder.llFhck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onFHCKClick(bean, holder.getAdapterPosition());
                }
            }
        });

        holder.llDw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String[] strings = {bean.getBaseName(), bean.getUnit_name()};
                DialogHelp.getSingleChoiceDialog(mContext, "单位", strings, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String lastUnit = holder.tvUnit.getText().toString().toString();
                        if (!strings[i].equals(lastUnit)) {
                            bean.setUnitType(i + "");
//                            double currCount = Double.parseDouble(holder.tvSfNum.getText().toString().trim());
                            String model2 = bean.getModel();
                            String[] split = model2.split("\\*");
                            int modle = Integer.parseInt(split[0]) * Integer.parseInt(split[1]);
                            if ("0".equals(bean.getUnitType())) {//0代表baseName 1代表unit_name
                                double count = ArithUtil.mul(Double.parseDouble(bean.getCount()), modle);
                                bean.setCount(count + "");
                                holder.tvUnit.setText(strings[i]);
                                holder.tvSfNum.setText(count + "");
                                holder.tvSalesSingle.setText(bean.getReprice() + "");
                                double mul = ArithUtil.mul(bean.getReprice(), count);
                                bean.setAllMoney(mul);
                                holder.tvSalesMoney.setText(mul + "");
                            } else if ("1".equals(bean.getUnitType())) {
                                double count = ArithUtil.div(Double.parseDouble(bean.getCount()), modle, 4);
                                bean.setCount(count + "");
                                holder.tvUnit.setText(strings[i]);
                                holder.tvSfNum.setText(count + "");
                                holder.tvSalesSingle.setText(bean.getSaleprice() + "");
                                double mul = ArithUtil.mul(bean.getSaleprice(), count);
                                bean.setAllMoney(mul);
                                holder.tvSalesMoney.setText(mul + "");
                            }
                        }
                        dialogInterface.dismiss();
                    }
                }).show();

//                if (listener != null) {
//                    listener.onDwClick(bean, holder.tvUnit, position);
//                }
            }
        });

        ((BackViewHolder) holder).itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                DialogHelp.getConfirmDialog(mContext, "确定删除  " + bean.getName() + "  么？", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        onItemDismiss(holder.getAdapterPosition(), bean);
                    }
                }).show();
                return true;
            }
        });

    }

    @Override
    public int getItemCount() {
        return workInfos.size();
    }


    class BackViewHolder extends RecyclerView.ViewHolder implements onStateChangedListener {
        @InjectView(R.id.tv_cc_code)
        TextView tvCcCode;
        @InjectView(R.id.tv_cc_name)
        TextView tvCcName;
        @InjectView(R.id.tv_gg_modle)
        TextView tvGgModle;
        @InjectView(R.id.tv_unit)
        TextView tvUnit;
        @InjectView(R.id.tv_sf_num)
        TextView tvSfNum;
        @InjectView(R.id.tv_sales_single)
        TextView tvSalesSingle;
        @InjectView(R.id.tv_sales_money)
        TextView tvSalesMoney;
        @InjectView(R.id.tv_fh_ware)
        TextView tvFhWare;
        @InjectView(R.id.ll_fhck)
        LinearLayout llFhck;
        @InjectView(R.id.tv_position)
        TextView tvPosition;
        @InjectView(R.id.tv_note)
        TextView tvNote;

        @InjectView(R.id.ll_jf)
        LinearLayout llJF;
        @InjectView(R.id.ll_bz)
        LinearLayout llBz;
        @InjectView(R.id.ll_dw)
        LinearLayout llDw;
        @InjectView(R.id.ll_sfsl)
        LinearLayout llSfsl;
        @InjectView(R.id.ll_xsdj)
        LinearLayout llXsdj;
        @InjectView(R.id.ll_xsje)
        LinearLayout llXsje;
        @InjectView(R.id.ll_jskc)
        LinearLayout llJskc;

        public BackViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }

        @Override
        public void onItemSelected() {
            //设置item的背景颜色为浅灰色
            itemView.setBackgroundColor(Color.LTGRAY);
        }

        @Override
        public void onItemClear() {
            //恢复item的背景颜色
            itemView.setBackgroundColor(0);
        }
    }
}
